//
//  HomeBlurManager.m
//  meijie
//
//  Created by go me on 2022/2/24.
//  Copyright © 2022 美借. All rights reserved.
//

#import "HomeBlurManager.h"
#import <UIKit/UIKit.h>
@implementation HomeBlurManager

+ (instancetype)homeBlurManager{
    static HomeBlurManager * manager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[HomeBlurManager alloc]init];
    });
    return manager;
}

- (void)registerToMonitor{
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(addBlurEffect) name:UIApplicationWillResignActiveNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(removeBlurEffect) name:UIApplicationDidBecomeActiveNotification object:nil];
}

- (void)addBlurEffect{
    
    CGRect mainScreenFrame = [UIScreen mainScreen].bounds;
    UIView *mainView = [[UIView alloc] initWithFrame:mainScreenFrame];
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:mainScreenFrame];
    UIView *currentView = [self getCurrentVC].view;
    
    //参数1:想要渲染的图片的尺寸。参数2：用来指定所生成图片的背景是否为不透明。参数3:表示位图的缩放比例，0根据屏幕的分辨率而变化。
    UIGraphicsBeginImageContextWithOptions(currentView.bounds.size, YES, 0);
    [currentView drawViewHierarchyInRect:currentView.bounds afterScreenUpdates:YES];
    UIImage *currentImgView = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    imageView.image = currentImgView;
    
    UIBlurEffect *blur = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
    UIVisualEffectView *effectView = [[UIVisualEffectView alloc] initWithEffect:blur];
    effectView.frame = mainScreenFrame;
    [imageView addSubview:effectView];
    mainView.tag = 1000;
    [mainView addSubview:imageView];
    [[self getWindow] addSubview:mainView];
    
}

- (void)removeBlurEffect{

for (UIWindow *window in [[UIApplication sharedApplication] windows]) {
    if (window.windowLevel == UIWindowLevelNormal) {
        UIView *view = [window viewWithTag:1000];
        [view removeFromSuperview];
    }
}
    
}

- (UIWindow *)getWindow{
    
    for (UIWindow *window in [[UIApplication sharedApplication] windows]) {
        if (window.windowLevel == UIWindowLevelNormal && CGRectEqualToRect(window.frame, [UIScreen mainScreen].bounds)) {
            return window;
        }
    }
    return  [UIApplication sharedApplication].delegate.window;
}

- (UIViewController *)getCurrentVC{
    
    UIViewController *viewController = nil;
    UIWindow * window = [[UIApplication sharedApplication] keyWindow];
    if (window.windowLevel != UIWindowLevelNormal) {
        NSArray *windows = [[UIApplication sharedApplication] windows];
        for(UIWindow * tmpWin in windows){
            if (tmpWin.windowLevel == UIWindowLevelNormal){
                window = tmpWin;
                break;
            }
        }
    }
    
    UIView *frontView = [[window subviews] objectAtIndex:0];
    id nextResponder = [frontView nextResponder];
    
    if ([nextResponder isKindOfClass:[UIViewController class]]) {
        viewController = nextResponder;
    } else {
        viewController = window.rootViewController;
    }
    
    if ([window.rootViewController isKindOfClass:[UITabBarController class]]) {
        UITabBarController *tabVC = (UITabBarController *)window.rootViewController;
        UINavigationController *navigationController = tabVC.selectedViewController;
        
        if (navigationController.viewControllers.count) {
            viewController = navigationController.viewControllers.lastObject;
        }
    }
    
    return viewController;
}


@end
